package br.org.espmessenger.modelo;

import java.util.Collection;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import br.org.espmessenger.middleware.InfraEstruturaException;

@Entity
public class Conversa {
	private int id;
	private Date inicio;
	private Collection<Usuario> participantes;
	private Collection<MensagemConversa> mensagens;
	
	public Conversa() {}

	public Conversa(Date inicio) {
		this.inicio = inicio;
	}

	@Id
	@GeneratedValue(generator="genconversa")
	@SequenceGenerator(name="genconversa", sequenceName="conversa_id_seq", allocationSize=1, initialValue=1)
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getInicio() {
		return inicio;
	}

	public void setInicio(Date inicio) {
		this.inicio = inicio;
	}

	@ManyToMany
	@JoinTable(name="usuario_na_conversa", 
			joinColumns=@JoinColumn(name="id_conversa"),
			inverseJoinColumns=@JoinColumn(name="id_usuario"))
	public Collection<Usuario> getParticipantes() {
		return participantes;
	}

	public void setParticipantes(Collection<Usuario> participantes) {
		this.participantes = participantes;
	}

	@OneToMany(mappedBy="conversa")
	public Collection<MensagemConversa> getMensagens() {
		return mensagens;
	}

	public void setMensagens(Collection<MensagemConversa> mensagens) {
		this.mensagens = mensagens;
	}

	public void adicionarParticipante(Usuario usuario) {
		participantes.add(usuario);
		usuario.getConversas().add(this);
	}

	public void notificarParticipantes(MensagemConversa mensagem) throws InfraEstruturaException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Conversa other = (Conversa) obj;
		if (id != other.id)
			return false;
		return true;
	}
	
	
	
}
